package com.douyu.data.up.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.douyu.data.common.CommonMessage;
import com.douyu.data.common.Constant;
import com.douyu.data.common.Resources;
import com.douyu.data.common.util.DateUtil;
import com.douyu.data.common.Result;
import com.douyu.data.up.entity.Up;
import com.douyu.data.up.mapper.UpMapper;
import com.douyu.data.up.model.UpListModel;
import com.douyu.data.up.model.UpModel;
import com.douyu.data.up.service.UpService;
import com.douyu.data.up.util.UpDetailFromDouYu;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class UpServiceImpl implements UpService {

    @Value("${douyu.url}")
    private String url;

    @Autowired
    private UpMapper upMapper;

    @Override
    public Result<UpModel> getUpDetail(String id) {
        Result<UpModel> result = new Result<>();
        UpModel upDetail = upMapper.getUpDetail(id);
        if(upDetail == null){
            return result.ng("这个主播不好找");
        } else {
            result.setResult(upDetail);
        }
        return result.ok();
    }

    @Override
    @Transactional
    public Result<String> addUp(String id) throws IOException, JSONException {
        Result<String> result = new Result<>();
        Up up = upMapper.selectOne(new QueryWrapper<Up>().eq("id", id));
        if(up != null){
            return result.ng(Resources.getProperty(CommonMessage.E00003));
        }
        Up request = UpDetailFromDouYu.request(url, id);
        request.setCreateBy("陈鹏");
        request.setCreateTime(DateUtil.dateFormat(new Date(), Constant.DATE_FORMAT_ONE));
        upMapper.insert(request);
        result.setResult(Resources.getProperty(CommonMessage.E00001));
        return result.ok();
    }

    @Override
    public UpListModel getUpList(String id, String name, String levelFrom, String levelTo, String association, String online, String attentionFrom, String attentionTo, String vipNumberFrom, String vipNumberTo, String diamondFansFrom, String diamondFansTo, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<UpModel> upList = upMapper.getUpList(id, name, levelFrom, levelTo, association, online, attentionFrom, attentionTo, vipNumberFrom, vipNumberTo, diamondFansFrom, diamondFansTo);
        PageInfo<UpModel> pageInfo = new PageInfo<>(upList);
        UpListModel upListModel = new UpListModel();

        upListModel.setTotal(pageInfo.getTotal());
        upListModel.setRecodes(pageInfo.getList());
        return upListModel;

    }
}
